﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CKEditor.NET;
using WebUI.BL.ProductCategories;
using WebUI.Models;
using WebUI.DAL;

namespace WebUI.AdminSite.Product
{
    public partial class ProductCategoryCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetDropDownList();
            }

        }

        protected void BtnClickUpload(object sender, UserControls.EventUploadImg e)
        {
            txtImg.Text = e.FileName;
        }

        public void GetDropDownList()
        {
            var repository = new ProductCategories();
            var result = repository.GetDropDownList((value, text) => new Models.ItemDropDownList()
                                                                         {
                                                                             Text = text,
                                                                             Value = value,
                                                                         }).ToList<ItemDropDownList>();
            if(result.Count==0)
            {
                result=new List<ItemDropDownList>
                           {
                               new ItemDropDownList
                                   {
                                       Text = "No parent",
                                       Value = "0"
                                   }
                           };
            }
            else
            {
                result.Insert(0, new ItemDropDownList
                {
                    Text = "No parent",
                    Value = "0"
                });
            }
                
            drpParent.DataSource = result;
            drpParent.DataTextField = "Text";
            drpParent.DataValueField = "Value";
            drpParent.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(Page.IsValid)
            {
                CKEditorControl controlEditor = UcNameDescription1.FindControl("ckey1") as CKEditorControl;
                string description = controlEditor.Text;

                TextBox txtName = UcNameDescription1.FindControl("txtName") as TextBox;
                string name = txtName.Text;
                var category = new tbl_ProductCategory
                                   {
                                       ParentCategoryNo = drpParent.SelectedValue=="0"?0:int.Parse(drpParent.SelectedValue),
                                       BreadCrum = txtBreadCrum.Text,
                                       CategoryImg = txtImg.Text,
                                       CreatedDate = DateTime.Now,
                                       CreatedUser = Request.Cookies["adler_userid"].Value,
                                       Description = description,
                                       Link = txtLink.Text,
                                       Name = name,
                                       Root=txtRoot.Text,
                                       DisplayOrder = String.IsNullOrEmpty(txtDisplayOrder.Text)?0:int.Parse(txtDisplayOrder.Text.Trim()),
                                       Status = drpStatus.SelectedValue
                                   };
                var repository = new ProductCategories();
                repository.Insert(category);
                Response.Redirect(Helpers.link.ToAdminProductCategory("ProductCategory.aspx"));
            }
        }
    }
}